package white_list

import (
	"bytes"
	"fmt"
	"net/http"
	"parking-lot-go-admin/config"
	"parking-lot-go-admin/controller"
	"parking-lot-go-admin/model"
	"parking-lot-go-admin/model/cars"

	"github.com/gin-gonic/gin"
)

func Insert(c *gin.Context) {
	requestDataStruct := struct {
		Name       string    `json:"name"`
		Mobile     string    `json:"mobile"`
		CarNumber  string    `json:"car_number"`
		CarNoColor int       `json:"car_no_color"`
		CarType    int       `json:"car_type"`
		OrderType  int       `json:"order_type"`
		OutTime    string `json:"out_time"`
	}{}

	c.ShouldBindJSON(&requestDataStruct)

	var carsWhiteListStruct cars.CarsWhiteListStruct

	model.CreateModelData(
		model.Db,
		carsWhiteListStruct.TableName(),
		map[string]interface{}{
			"name":         requestDataStruct.Name,
			"mobile":       requestDataStruct.Mobile,
			"car_number":   requestDataStruct.CarNumber,
			"car_no_color": requestDataStruct.CarNoColor,
			"car_type":     requestDataStruct.CarType,
			"order_type":   requestDataStruct.OrderType,
			"out_time":     requestDataStruct.OutTime,
		},
	)

	//绑定车牌异步请求
	go func() {
		//拼接url
		systemSection := config.Cfg.Section("SYSTEM")
		url := systemSection.Key("INTERFACE_URL").String() + "artificial/bind"

		jsonData := []byte(
			fmt.Sprintf(`{"phone": "%s", "car_id": "%s", "car_no_color": "%d", "car_type": "%d","park_type":"%d","type":"%d"}`,
				requestDataStruct.Mobile, requestDataStruct.CarNumber, requestDataStruct.CarNoColor, requestDataStruct.CarType, 1, 1))

		//发送请求
		resp, _ := http.Post(url, "application/json", bytes.NewBuffer(jsonData))
		defer resp.Body.Close()
	}()

	controller.Success(c, "")
}
